package com.hkyp.ss.course.web.controller;

import com.hkyp.ss.course.apis.CourseService;
import com.hkyp.ss.course.apis.NavigationItemService;
import com.hkyp.ss.course.apis.dto.request.NavigationItemDTO;
import com.hkyp.ss.course.apis.dto.response.CourseVO;
import com.hkyp.ss.course.apis.dto.response.NavigationItemVO;
import com.hkyp.ss.course.apis.exception.UnacceptableException;
import com.hkyp.ss.course.apis.utils.BeanMapper;
import com.hkyp.ss.course.web.dto.base.ResultBean;
import com.hkyp.ss.course.web.dto.request.WebNavigationItemDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

@Api(description = "导航栏")
@Slf4j
@RestController
@RequestMapping(value = "/a/h/")
public class NavigationItemController {

    @Reference(version = "1.0")
    private NavigationItemService navigationItemService;

    @Reference(version = "1.0")
    private CourseService courseService;

    /**
     * 增加导航节点
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "新增导航节点(预留)")
    @RequestMapping(value = "/NavigationItem", method = RequestMethod.POST)
    public ResultBean addNavigationItem(@Valid @RequestBody WebNavigationItemDTO navigationItemDTO){

        navigationItemService.addNavigationItem(BeanMapper.map(navigationItemDTO,NavigationItemDTO.class));
        return new ResultBean<>();

    }

    /**
     * 导航栏节点列表
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "导航栏节点列表")
    @RequestMapping(value = "/NavigationItem/list", method = RequestMethod.GET)
    public ResultBean getNavigationItemList(){

        return new ResultBean<>(navigationItemService.getAllNavigationItemList());

    }

    /**
     * 导航栏节点列表
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "查询该课程的各级导航名称")
    @RequestMapping(value = "/NavigationItem/hierarchy/{cid}", method = RequestMethod.GET)
    public ResultBean getNavigationItemByCid(@PathVariable Long cid){

        CourseVO courseVO = courseService.selectCourseNaked(cid);
        if(courseVO == null){
            return new ResultBean<>(new ArrayList<>());
        }
        Long category = courseVO.getCategory();
        if(category == null){
            throw new UnacceptableException("类别码为空");
        }
        long modSec = category%100;
        long modFir = category%10000;
        long secCode = category - modSec;
        long firCode = category - modFir;
        List<Long> list = new ArrayList<>();
        list.add(firCode);
        list.add(secCode);
        list.add(category);
        List<NavigationItemVO> itemVOList = navigationItemService.getNavigationItemsByCodes(list);
        // 从小到大排列， 对应着等级从高到低排列
        itemVOList.sort(Comparator.comparing(NavigationItemVO::getCode));
        NavigationItemVO navigationItemVO = new NavigationItemVO();
        navigationItemVO.setCode(cid);
        navigationItemVO.setValue(courseVO.getTitle());
        itemVOList.add(navigationItemVO);
        return new ResultBean<>(itemVOList);

    }


}
